require 'MVVM.Sources.View.Adaptor'

SpriteAdaptor = class('SpriteAdaptor', Adaptor)

function SpriteAdaptor:ctor(gameObject, propertyName, binder, pathFunction)

    self.component = gameObject:GetComponent('SpriteRenderer')
    self.pathFunction = pathFunction
    self:BindProperty(propertyName, binder)
    self.loadCo = nil
    self.loadedAsset = nil
end

function SpriteAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if oldValue == newValue then
        return
    end

    if self.loadCo then
        coroutine.stop(self.loadCo)
    end

    if newValue == nil then
        return
    end

    --self.loadCo = coroutine.start(function()
    --print('#####################', debug.traceback())
    local path = nil
    local name = nil
    if self.pathFunction then
        path, name = self.pathFunction(newValue)
    else
        path = newValue
    end
    if path==nil then return end

    local asset = RESM.LoadAsset(path, name, typeof(UnityEngine.Sprite))
    asset:Require(self.component.gameObject)
    self.component.sprite = asset.resource
    --self.loadCo = nil
    --end)
end